/**
 * 
 */
package org.istt.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.istt.dao.FileDao;
import org.istt.dao.FileMgrDao;
import org.istt.dao.WorkflowProcessDao;
import org.istt.domain.FileManagement;
import org.istt.domain.User;
import org.istt.domain.WorkflowProcess;
import org.istt.service.FileMgrManager;
import org.istt.web.helper.ExtendedPaginatedList;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Huy
 *
 */
@Service("fileMgrManager")
public class FileMgrManagerImpl extends ManagerImpl<FileManagement, Long> implements FileMgrManager {
	
	private FileDao fileDao;
	private WorkflowProcessDao workflowProcessDao;
	
	/**
	 * @param fileDao
	 */
	@Autowired
	public void setFileDao(FileDao fileDao) {
		this.fileDao = fileDao;
	}
	
	/**
	 * @param workflowProcessDao the workflowProcessDao to set
	 */
	@Autowired
	public void setWorkflowProcessDao(WorkflowProcessDao workflowProcessDao) {
		this.workflowProcessDao = workflowProcessDao;
	}

	/**
	 * @param categoryDao the categoryDao to set
	 */
	@Autowired
	public void setFileMgrDao(FileMgrDao dao) {
		this.dao = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileMgrManager#search(java.lang.Long, java.lang.String, java.util.Date)
	 */
	@Override
	public List<List<Object>> search(Long fieldId, String content, Date toDate) {
		if (fieldId == 0l) fieldId = null;
		return ((FileMgrDao)dao).find(fieldId, content, toDate);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileMgrManager#countByFileId(java.lang.String)
	 */
	@Override
	public int countByFileId(String fileId) {
		return ((FileMgrDao)dao).countByFileId(fileId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileMgrManager#updateToEndStatus(java.lang.String[], java.lang.String[], org.istt.domain.User)
	 */
	@Override
	public void updateToEndStatus(String[] fileIds, String[] fmgrIds, User approver) {
		List<FileManagement> fmgrs = new ArrayList<FileManagement>();
		for (int i = 0; i < fileIds.length; i++) {
			FileManagement fmgr = new FileManagement();
			fmgr.setApprovedDate(new DateTime());
			fmgr.setApprover(approver);
			fmgr.setFile(fileDao.findById(fileIds[i], false));
			fmgr.setSender(approver);
			fmgr.setWorkflowProcess(workflowProcessDao.getFinalStepOfWorkflow(fileDao.getWorkFlowId(fileIds[i])));
			fmgrs.add(fmgr);
		}
		dao.saveEntities(fmgrs);
		((FileMgrDao)dao).approveOfLatestFile(fmgrIds, approver);
	}
	
	@Override
	public List<FileManagement> searchFileMgrs(String fileId, Boolean activeStatus, Integer position) {
		return ((FileMgrDao)dao).findFileMgrs(fileId, activeStatus, position);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileMgrManager#rollback(java.lang.Long, java.lang.Long)
	 */
	@Override
	public void rollback(Long currentStepId, Long previousStepId) {
		FileManagement currentStep = get(currentStepId);
		
		if (currentStep.getWorkflowProcess().getPosition() == WorkflowProcess.START_POSITION) return;
		FileManagement previousStep = get(previousStepId);
		
		previousStep.setApprovedDate(null);
		previousStep.setApprovedComment(null);
		previousStep.setActiveStatus(false);
		previousStep.setApproveStatus(null);
		
		remove(currentStepId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileMgrManager#getAmendmentFile()
	 */
	@Override
	public ExtendedPaginatedList getAmendmentFile(Long userId, ExtendedPaginatedList paginatedList) {
		
		List<FileManagement> results = ((FileMgrDao)dao).getAmendmentFile(userId, paginatedList.getFirstRecordIndex(), 
																			paginatedList.getPageSize(), 
																			paginatedList.getSortDirection(), 
																			paginatedList.getSortCriterion());
			
        paginatedList.setList(results);
        paginatedList.setTotalNumberOfRows(((FileMgrDao)dao).getAmendmentFileRecordCount(userId));
		
		return paginatedList;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileMgrManager#removeAmendment(java.lang.Long[])
	 */
	@Override
	public void removeAmendment(Long[] ids) {
		((FileMgrDao)dao).removeAmendment(ids);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileMgrManager#getCurrentStep(java.lang.String)
	 */
	@Override
	public int getCurrentStep(String fileId) {
		return ((FileMgrDao)dao).getCurrentStep(fileId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.istt.service.FileMgrManager#getCurrentStepProcess(java.lang.String)
	 */
	@Override
	public FileManagement getCurrentStepProcess(String fileId) {
		return ((FileMgrDao)dao).getCurrentStepProcess(fileId);
	}

	@Override
	public int getAmendmentFileRecordCount(Long userId) {
		// TODO Auto-generated method stub
		return ((FileMgrDao)dao).getAmendmentFileRecordCount(userId);
	}
	
}
